package com.jxb.tranction;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/11/26 21:33
 **/
@Configuration//这里不加configuration，就会导入不同的数据源（因为是Full配置类才会去处理）
@EnableTransactionManagement
@ComponentScan(basePackages = "com.jxb.tranction")
public class TrxConfig {

    @Bean
    public JdbcTemplate jdbcTemplate() {
    	return new JdbcTemplate(dataSource());
    }


    @Bean
    public PlatformTransactionManager transactionManager() {
    	DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
    	transactionManager.setDataSource(dataSource());
    	transactionManager.setGlobalRollbackOnParticipationFailure(false);
    	return transactionManager;
    }


    @Bean
    public DataSource dataSource() {
    	DriverManagerDataSource dataSource = new DriverManagerDataSource();
    	dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC");
    	dataSource.setUsername("root");
    	dataSource.setPassword("123456");
    	return dataSource;
    }

}
